package com.weather;
/**
 *
 **/

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import javax.enterprise.context.ApplicationScoped;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
 * @Author weiwei
 * @Date 2022-03-16 09:50
 * @description
 **/
@ApplicationScoped
public class WeatherService {


    public WeatherService() {
    }



//    public String getCityWeather(String city) throws Exception {
//        String url = "http://wthrcdn.etouch.cn/weather_mini?city=" + city;
//        HttpClient client = HttpClient.newHttpClient();
//        HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).build();
//        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
//        String ret = response.body();
//        System.out.println("查询天气数据：" + ret);
//        return ret;
//    }

    public String getCityWeather(String city) throws Exception {
        String url = "http://wthrcdn.etouch.cn/weather_mini?city=" + city;
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(url)
                .build();
        final Call call = okHttpClient.newCall(request);
        Response response = call.execute();
        String ret =  response.body().string();
        System.out.println("查询天气数据：" + ret);
        return ret;
    }

}
